import { defineStore } from 'pinia'

interface User {
    id: string
    username: string
    role: 'creator' | 'respondent'
}

export const useAuthStore = defineStore('auth', {
    state: () => ({
        currentUser: null as User | null,
        isAuthenticated: false
    }),
    actions: {
        login(username: string, role: 'creator' | 'respondent') {
            this.currentUser = {
                id: Date.now().toString(),
                username,
                role
            }
            this.isAuthenticated = true
            localStorage.setItem('auth', JSON.stringify(this.currentUser))
        },
        logout() {
            this.currentUser = null
            this.isAuthenticated = false
            localStorage.removeItem('auth')
        },
        initialize() {
            const auth = localStorage.getItem('auth')
            if (auth) {
                this.currentUser = JSON.parse(auth)
                this.isAuthenticated = true
            }
        }
    }
})
